﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge.Imaging.Filters;

namespace HomeDms.Imaging
{
    public partial class BrightnessContrastToolPanel : ImageToolPanel
    {
        public BrightnessContrastToolPanel()
        {
            InitializeComponent();
        }

        private void SetBrightnessAndContrast()
        {
            ContrastCorrection con = new ContrastCorrection((double)nudContrast.Value);
            Bitmap bmp = Bitmap; // get original because we gonna use 2 filters...
            bmp = con.Apply(bmp);
            BrightnessCorrection bri = new BrightnessCorrection((double)nudBrightness.Value);
            Bitmap = bri.Apply(bmp);            
            InvalidateImage();
        }

        protected override void FinalApply()
        {
            SetBrightnessAndContrast();
        }

        protected override void Execute()
        {
            SetBrightnessAndContrast();
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            nudBrightness.Value = (decimal)tbBrightness.Value / 1000;
            Changed();
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            tbBrightness.Value = (int)Math.Round(nudBrightness.Value * 1000);
            Changed();
        }

        private void tbContrast_Scroll(object sender, EventArgs e)
        {
            nudContrast.Value = (decimal)tbContrast.Value / 1000;
            Changed();
        }

        private void nudContrast_ValueChanged(object sender, EventArgs e)
        {
            tbContrast.Value = (int)Math.Round(nudContrast.Value * 1000);
            Changed();
        }
    }
}
